package com.aisino.util;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.aisino.entity.TOrderData;
import com.aisino.entity.TSysorderline;

public class HandleDataUtil {
	static final String maxMoney = "100000";

    static final BigDecimal max = new BigDecimal(maxMoney);

    static final BigDecimal maxChaiFenJe = new BigDecimal("99999");
	
	public static void chaifenHang(List<TOrderData> list, List<List<TOrderData>> newList) throws Exception {
        // 暂存一张新发票
        List<TOrderData> tempHangList = new ArrayList<>();
        // 暂存一张发票的金额
        BigDecimal tempFaPiaoJe = new BigDecimal("0");
        // 上面步骤后将没有单行超限
        for (int j = 0; j < list.size(); j++) {
            TOrderData mapTemp = list.get(j);
            BigDecimal tempJe = mapTemp.getJE();
            // 累加发票金额
            tempFaPiaoJe = BigDecimalUtil.add(tempFaPiaoJe, tempJe);
            // 如果次发票加上这一行金额已经超限
            if (tempFaPiaoJe.compareTo(maxChaiFenJe) >= 0) {
                List<TOrderData> addList = new ArrayList<>();
                // 深拷贝复制内存
                addList.addAll(tempHangList);
                // 添加到发票集合
                newList.add(addList);
                // 清空临时集合
                tempHangList.clear();
                // 增加一个新的明细行
                TOrderData map1 = new TOrderData();
                // 深拷贝复制内存
                map1 = (TOrderData) mapTemp.deepClone();
                tempHangList.add(map1);
                // 重置临时金额为这第一行的金额
                tempFaPiaoJe = (BigDecimal) list.get(j).getJE();

            } else {
                // 没有超限
                TOrderData map1 = new TOrderData();
                // 深拷贝复制内存
                map1 = (TOrderData) mapTemp.deepClone();
                tempHangList.add(map1);
            }
        }
        // 将最后一张没有加到发票集合的发票加入
        newList.add(tempHangList);
    }
	/**
	 * 
	 * @Title: compareMap 比较两个map中的字段是否相等
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param: @param mapOld 会被加入数据库的map
	 * @param: @param mapNew 带比较的map
	 * @param: @return
	 * @return: boolean
	 * @throws
	 */
	public static boolean compareMap(Map<String, Object> mapOld,
			Map<String, Object> mapNew) {
		// 需要比较的map中的字段
		// 万一需要比较很多字段，做两手准备
		// List<String> compareList = new LinkedList<>(Arrays.asList("ohid",
		// "dj",
		// "dw", "fhr", "fpzl", "gfdzdh", "gfhm", "gfmc", "gfsh",
		// "gfyhzh", "ggxh", "hsbz", "skr", "slv", "sphm", "spmc",
		// "ssflbm", "mobile", "email"));

		// 对具有相同购方、相同商品、相同单价的商品行订单进行单据合并
		List<String> compareList = new LinkedList<>(Arrays.asList("DJHM","GFHM","GFMC","GFSH","GFYHZH","GFDZDH",
				"SPMC", "GGXH", "DW", "DJ", "SPHM", "SLV", "SSFLBM"));
		for (String key : compareList) {
			// 当某一个键值对没有这个key则直接不相等
			if (!mapOld.containsKey(key) || !mapNew.containsKey(key)) {
				return false;
			}
			// 一个为空如果另外一个不为空则不等
			if (mapOld.get(key) == null) {
				if (mapNew.get(key) != null) {
					return false;
				}
				// 忽略两者都为空的情况
				// 比较两者都不为空的情况
			} else if (mapOld.get(key) != null && mapNew.get(key) != null) {
				if (!mapOld.get(key).toString().trim().equals(mapNew.get(key).toString().trim())) {
					// 不为空则直接比较
					return false;
				}
			}
		}
		return true;
	}
	/**
	 * 
	 * @Title: jeAndSlAdd
	 * @Description: 将所传入的字段进行相加
	 * @param: @param mapOld
	 * @param: @param mapNew
	 * @param: @param addString
	 * @return: void
	 * @throws
	 */
	public static void jeAndSlAdd(Map<String, Object> mapOld,
		Map<String, Object> mapNew, String addString) {
		BigDecimal sl = (BigDecimal)mapOld.get(addString);
		BigDecimal sl1 =(BigDecimal)mapNew.get(addString);
		// 如果数量都不为空则相加
		if (sl != null && sl1 != null) {
			mapOld.put(addString, BigDecimalUtil.add(sl, sl1).toString());
		} else {
			if (sl != null) {
				mapOld.put(addString, sl.toString());
			} else {
				mapOld.put(addString, sl1.toString());
			}
		}
	}
	public static void soutData(List<List<TOrderData>> newList) {
        for (int q = 0; q < newList.size(); q++) {
            System.out.println("一张票");
            for (int w = 0; w < newList.get(q).size(); w++) {
                System.out.println(w + "行");
                System.out.print("金额" + newList.get(q).get(w).getJE().toString() + "     ");
                System.out.println();
            }
        }
    }

	/**
	 * 查询条件 DJHM ODID
	 * Page-页数 Limit-每页条数
	 * @return
	 */
	public static void handleLayuiDate(Map<String,String>map2,String TimeString,int i){
		//时间不为空 编辑数据
		String[] timeStrArr = TimeString.split(" ~ ");
		String biginTimeString = timeStrArr[0];
		String endTimeString = timeStrArr[1];
		System.out.println(biginTimeString+"*******"+endTimeString);
		biginTimeString = biginTimeString.replace("/", "");
		endTimeString = endTimeString.replace("/", "");
		if(i==0){
			map2.put("kaiPiaoBegin", biginTimeString);
			map2.put("KaiPiaoEnd", endTimeString);
		}else{
			map2.put("getDataBegin", biginTimeString);
			map2.put("getDataEnd", endTimeString);
		}
	}


	/* 根据属性名获取属性值
	 * */
	private static Object getFieldValueByName(String fieldName, Object o) {
		try {
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			Method method = o.getClass().getMethod(getter, new Class[] {});
			Object value = method.invoke(o, new Object[] {});
			return value;
		} catch (Exception e) {
			return null;
		}
	}

	public static void main(String[] args) {
		List<String> compareList = new LinkedList<>(Arrays.asList("DJHM","GFHM","GFMC","GFSH","GFYHZH","GFDZDH",
				"SPMC", "GGXH", "DW", "DJ", "SPHM", "SLV", "SSFLBM"));
		TSysorderline tSysorderline=new TSysorderline();
		tSysorderline.setSPMC("SPMC123");
		tSysorderline.setGGXH("GGXH");
		tSysorderline.setGGXH("DW");
		tSysorderline.setDJ(new BigDecimal("123.123"));
		tSysorderline.setSPHM("SPHM");
		tSysorderline.setSLV(new BigDecimal("123.123"));
		tSysorderline.setSSFLBM("SSFLBM");
		Object SPMC = getFieldValueByName("SPMC", tSysorderline);
		Object DJ=getFieldValueByName("DJ", tSysorderline);
		System.out.println(SPMC.toString()+DJ.toString());
	}

}
